﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using MapObjects;

namespace MOClient
{
    public partial class MainForm
    {
        /////////////////////////////////////////////////////////////////////////////
        /////////////////////// DoMapExtent ////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////
        void DoMapExtent(string imgGuid, string username, MapObjects.Rectangle ext)
        {
            //f_bZoomSelected:=TRUE;
            //f_bZoomOutSelected:=false;
            //f_bPanSelected:=false;
            //f_bLayerVisibilitySelected=false;

            axMap1.Extent = ext;
            CreateHTML(imgGuid, username, ext);
            //CreateBinaryResponse(username, axMap1.Extent);
            //CreateIMG(username, axMap1.Extent);
        }


        /////////////////////////////////////////////////////////////////////////////
        /////////////////////// DoLayerVisibility ////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////
        void DoLayerVisibility(string imgGuid, string username,
        MapObjects.Rectangle ext,
        bool layer1Visible,
        bool layer2Visible,
        bool layer3Visible,
        bool layer4Visible,
        bool layer5Visible,
        bool layer6Visible)
        {
            //f_bZoomSelected=false;
            //f_bZoomOutSelected=false;
            //f_bPanSelected=false;
            //f_bLayerVisibilitySelected=true;

            layer1.Visible = layer1Visible;
            layer2.Visible = layer2Visible;
            layer3.Visible = layer3Visible;
            layer4.Visible = layer4Visible;
            layer5.Visible = layer5Visible;
            layer6.Visible = layer6Visible;

            axMap1.Extent = ext;
            CreateHTML(imgGuid, username, ext);

        }

        /////////////////////////////////////////////////////////////////////////////
        /////////////////////// DoZoomIn ////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////

        void DoZoomIn(string imgGuid, string username, MapObjects.Rectangle ext, long x, long y)
        {
            Point oPt, oCtr;

            // f_bZoomSelected=TRUE;
            // f_bZoomOutSelected=false;
            // f_bPanSelected=false;
            //f_bLayerVisibilitySelected=false;

            oPt = ConvertClick(ext, x, y);
            oCtr = ext.Center;
            ext.ScaleRectangle(0.5);
            ext.Offset(oPt.x - oCtr.x, oPt.y - oCtr.y);
            CreateHTML(imgGuid, username, ext);
            oPt = null;
            oCtr = null;
        }

        /////////////////////////////////////////////////////////////////////////////
        /////////////////////// DoZoomOut ///////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////

        void DoZoomOut(string imgGuid, string username, MapObjects.Rectangle ext, long x, long y)
        {
            Point oPt, oCtr;

            // f_bZoomSelected=false;
            // f_bZoomOutSelected=TRUE;
            // f_bPanSelected=false;
            //f_bLayerVisibilitySelected=false;

            oPt = ConvertClick(ext, x, y);
            oCtr = ext.Center;
            ext.ScaleRectangle(2);
            ext.Offset(oPt.x - oCtr.x, oPt.y - oCtr.y);
            CreateHTML(imgGuid, username, ext);
            oPt = null;
            oCtr = null;
        }

        /////////////////////////////////////////////////////////////////////////////
        /////////////////////// DoPan ///////////////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////

        void DoPan(string imgGuid, string username, MapObjects.Rectangle ext, long x, long y)
        {
            Point oPt, oCtr;

            // f_bZoomSelected=false;
            // f_bZoomOutSelected=false;
            // f_bPanSelected=true;
            //f_bLayerVisibilitySelected=false;

            oPt = ConvertClick(ext, x, y);
            oCtr = ext.Center;
            ext.Offset(oPt.x - oCtr.x, oPt.y - oCtr.y);

            CreateHTML(imgGuid, username, ext);
            oPt = null;
            oCtr = null;
        }

        /////////////////////////////////////////////////////////////////////////////
        /////////////////////////// ConvertClick /////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////
        Point ConvertClick( MapObjects.Rectangle ext, long x, long y)
        {
            Point Result;
            axMap1.Extent = ext;
            Result = axMap1.ToMapPoint(x, y);
            return Result;
        }

        /////////////////////////////////////////////////////////////////////////////
        /////////////////////////// CreateHTML /////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////
        void CreateHTML(string imgGuid, string username, MapObjects.Rectangle ext)
        {
            string imgURL = string.Empty;

            imgURL = f_strTmpPath + username + @"/" + CreateMap(imgGuid, username, ext);

            #region Tmp.

            axWebLink1.WriteResponseHeader("Content-type: text/html" + "\r\n" + "\r\n");
            axWebLink1.WriteString("<HTML><HEAD>" + "\r\n");
            axWebLink1.WriteString("<TITLE>Використання MapObjects IMS</TITLE>" + "\r\n");
            axWebLink1.WriteString("<meta http-equiv='Content-Type' content='text/html; charset=windows-1251'>" + "\r\n");
            axWebLink1.WriteString("<script language='javascript'>" + "\r\n");

            axWebLink1.WriteString("function statusmessageobject(p,d) {" + "\r\n");
            axWebLink1.WriteString("  this.msg = message" + "\r\n");
            axWebLink1.WriteString("  this.out = ''" + "\r\n");
            axWebLink1.WriteString("  this.pos = position" + "\r\n");
            axWebLink1.WriteString("  this.delay = delay" + "\r\n");
            axWebLink1.WriteString("  this.i     = 0" + "\r\n");
            axWebLink1.WriteString("  this.reset = clearmessage}" + "\r\n");

            axWebLink1.WriteString("function clearmessage() { this.pos = position} " + "\r\n");
            axWebLink1.WriteString("var position = 100" + "\r\n");
            axWebLink1.WriteString("var delay    = 40" + "\r\n");
            axWebLink1.WriteString("var message  = 'Приклад використання MapObjects IMS'" + "\r\n");
            axWebLink1.WriteString("var scroll = new statusmessageobject()" + "\r\n");

            axWebLink1.WriteString("function scroller() {for (scroll.i = 0; scroll.i < scroll.pos; scroll.i++) { scroll.out += '' }" + "\r\n");
            axWebLink1.WriteString("    if (scroll.pos >= 0) scroll.out += scroll.msg" + "\r\n");
            axWebLink1.WriteString("  else scroll.out = scroll.msg.substring(-scroll.pos,scroll.msg.length)" + "\r\n");
            axWebLink1.WriteString("  window.status = scroll.out" + "\r\n");
            axWebLink1.WriteString("  scroll.out =''" + "\r\n");
            axWebLink1.WriteString("  scroll.pos--" + "\r\n");
            axWebLink1.WriteString("    if (scroll.pos < -(scroll.msg.length)) { scroll.reset() }" + "\r\n");
            axWebLink1.WriteString("    setTimeout ('scroller()',scroll.delay)}" + "\r\n");

            axWebLink1.WriteString(" </script> </HEAD>" + "\r\n");

            axWebLink1.WriteString("<BODY  BGCOLOR=Gray onLoad='scroller()'>" + "\r\n");

            axWebLink1.WriteString("<FORM>" + "\r\n");
            axWebLink1.WriteString("<INPUT TYPE = hidden NAME=name Value=" + name + ">" + "\r\n");
            axWebLink1.WriteString("<INPUT TYPE = hidden NAME=cmd Value=mouseclick>" + "\r\n");
            axWebLink1.WriteString("<INPUT TYPE = hidden NAME=username Value=" + username + ">" + "\r\n");
            axWebLink1.WriteString("<INPUT TYPE = hidden NAME=left Value=" + Math.Floor(ext.Left) + ">" + "\r\n");
            axWebLink1.WriteString("<INPUT TYPE = hidden NAME=bottom Value=" + Math.Floor(ext.Bottom) + ">" + "\r\n");
            axWebLink1.WriteString("<INPUT TYPE = hidden NAME=right Value=" + Math.Floor(ext.Right) + ">" + "\r\n");
            axWebLink1.WriteString("<INPUT TYPE = hidden NAME=top Value=" + Math.Floor(ext.Top) + ">" + "\r\n");

            axWebLink1.WriteString("<TABLE> <TR> <TD width = 210>" + "\r\n");

            //axWebLink1.WriteString("<BR><INPUT TYPE=RADIO NAME=cmd VALUE=zoomin "+IIF(f_bZoomSelected)+"> Збільшення карти"+"\r\n");
            //axWebLink1.WriteString("<BR><INPUT TYPE=RADIO NAME=cmd VALUE=zoomout "+IIF(f_bZoomOutSelected)+"> Зменшення карти"+"\r\n");
            //axWebLink1.WriteString("<BR><INPUT TYPE=RADIO NAME=cmd VALUE=pan "+IIF(f_bPanSelected)+"> Переміщення карти"+"\r\n");
            axWebLink1.WriteString("<BR><A HREF=esrimap.dll?name=" + name
                + "&username=" + username
                + "&cmd=map> Показати всю карту </A>" + "\r\n");

            axWebLink1.WriteString("</TD><TD><CENTER>" + "\r\n");

            #endregion

            //axWebLink1.WriteString("ImG SRC=" + imgURL);

            axWebLink1.WriteString("<BR><BR><INPUT TYPE=image NAME=click SRC=" + imgURL + "></CENTER>");

            #region Tmp.

            axWebLink1.WriteString("</TD></TR></TABLE>" + "\r\n");

            axWebLink1.WriteString("</FORM>" + "\r\n");
            axWebLink1.WriteString("</BODY>" + "\r\n");
            axWebLink1.WriteString("</HTML>" + "\r\n");

            #endregion
        }

        /////////////////////////////////////////////////////////////////////////////
        /////////////////////////// CreateBinaryResponse /////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////
        void CreateBinaryResponse(string imgGuid, string username, MapObjects.Rectangle ext)
        {
            string imgURL = string.Empty;

            imgURL = f_strTmpPath + username + @"/" + CreateMap(imgGuid, username, ext);

            axWebLink1.WriteFile(imgURL);
        }

        /////////////////////////////////////////////////////////////////////////////
        /////////////////////////// CreateIMG /////////////////////////////////////
        /////////////////////////////////////////////////////////////////////////////
        void CreateIMG(string imgGuid, string username, MapObjects.Rectangle ext)
        {
            string imgURL = string.Empty;

            imgURL = f_strTmpPath + username + @"/" + CreateMap(imgGuid, username, ext);
            axWebLink1.WriteString("<INPUT TYPE=image NAME=click SRC=" + imgURL + ">");
        }    
    }
}

